// 03admin/03waiter/agreeTo/agreeTo.js

var co = require('../../../resource/js/common.js')
var app = getApp()
var _this, _data

var _inputPhone, _Sms_Code
var _unionid = wx.getStorageSync('unionid')
var _session
var _openid
var _opt = {}
//是否取消倒计时
var isCancelCount = 0

Page({
   data: {
      Biz_ID: '', //店铺ID
      user: 0, //用户信息
      Verified_Phone: '', //验证的手机号,初始为0，自动登录后变为非0，以确认要否显示微信授权
      getSmsCodeText: '获取验证码', //验证码
      Verified_Ok: '', //验证正确否
      isCode: 0, //验证码0错误，1正确
   },

   onLoad(opt) {
      wx.showLoading({
         title: '加载中…',
      })
      _this = this
      _data = this.data

      //判断参数
      if (co.empty(opt.scene)) {
         co.Modal('缺少“opt.scene”参数')
         return
      }
      if (opt.scene.indexOf("BID.") == -1) {
         co.Modal('“opt.scene”参数缺少“BID.”')
         return
      }
      _opt = app.do_opt_scene_data(opt)
      console.log('添加成员_opt====', _opt)
      _this.autoLogin()
   },

   //自动登录
   autoLogin() {
      //获取session值
      _session = _session ? _session : wx.getStorageSync('session')
      console.log('自动登录_session====', _session)
      //参数
      var url = 'user/Mg/autoLogin'
      var param = {
         // unionid: _session.unionid,
         openid: _session.openid,
         appid: app.Data.app.appid
      }
      co.getShareApi(url, param, function(res) {
         console.log('自动登录res===', res)
         //处理自动登录的用户数据
         _this.doUserData(res.data)
         wx.hideLoading()
      })
   },
   //处理自动登录的用户数据
   doUserData(data) {
      console.log('data====', data)
      if (co.empty(data.userInfo)) {
         this.setData({
            user: '',
            Verified_Ok: 0,
            Verified_Phone: ''
         })
      } else {
         //获取用户信息
         var userInfo = data.userInfo
         app.Data.userInfo = userInfo
         //刷新页面
         _this.setData({
            user: userInfo,
            Verified_Ok: app.checkPhoneFormat(userInfo.User_Mobile),
            Verified_Phone: userInfo.User_Mobile
         })
      }
      //验证通过(已经是会员,并且有电话号码了)
      if (_data.Verified_Ok) {
         //验证是否在该商家已经成了服务员
         _this.isWaiter(data.biz_list)
      }
      console.log('app.Data.userInfo===', app.Data.userInfo)
   },
   //查看是否存在这个服务员
   isWaiter(bizList) {
      var url = 'waiter/Wmg/isWaiter'
      //参数
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         Biz_ID: _opt.BID
      }
      co.getShareApi(url, param, function(res) {
         if (res.code === 0) {
            //提示信息
            co.confirm('', res.msg, '', '确定', function(_res) {
               wx.redirectTo({
                  url: '/04jyhs/admin/home',
               })
            })
         }
      })
   },
   //添加服务员
   save(e) {
      var name = e.detail.value.name
      if (co.empty(name)) {
         co.toastAllText('姓名不能为空, 请重新输入')
         return
      }
      //参数
      var param = {
         Biz_ID: _opt.BID,
         User_ID: app.Data.userInfo.User_ID,
         unionid: app.Data.userInfo.unionid,
         name: name
      }
      //添加服务员
      co.getShareApi('waiter/Wmg/save', param, function(res) {
         if (res.code == 0) {
            co.confirm('', res.msg, '', '确定', function(_res) {
               //选择商家店铺
               wx.redirectTo({
                  url: '/04jyhs/admin/home',
               })
            })
         } else {
            co.toastAllText(res.msg)
         }
      })
   },
   //一键获取微信认证手机
   getPhoneNumber(e) {
      app.decryptWxInfoData(e.detail, function(res) {
         _this.setData({
            Verified_Ok: 1,
            Verified_Phone: res.phoneNumber
         })
      })
   },
   //手机输入框事件
   inputPhone(e) {
      _inputPhone = e.detail.value
      console.log('【inputPhone(e)】===' + _inputPhone)
   },
   //换个手机
   changePhone(e) {
      _this.setData({
         Verified_Ok: 0,
         Verified_Phone: ''
      })
   },
   //输入验证码
   inputCode(e) {
      console.log('输入码==' + e.detail.value)
      if (_Sms_Code == e.detail.value) {
         //取消倒计时
         isCancelCount = 1
         //刷新页面
         _this.setData({
            Verified_Ok: 1,
            Verified_Phone: _data.Verified_Phone
         })
      }
   },
   //获取短信验证码
   getPhoneCode() {
      if (!app.checkPhoneFormat(_inputPhone)) {
         co.toastAllText('手机号格式不正确')
         return
      }
      _data.Verified_Phone = _inputPhone
      // _this.setData({
      //    Verified_Phone: _inputPhone
      // })
      co.getShareApi('api/Sms/getPhoneCode', {
         mobile: _inputPhone,
         type: 'user_reg',
         appid: "1400134010"
      }, function(res) {
         if (res.code == 0) {
            //获取验证码
            _Sms_Code = res.data.Sms_Code
            //倒计时
            _this.countDown(60)
         } else {
            co.toastAllText(res.msg)
         }
      })
   },
   //倒计时
   countDown(time) {
      var second = ''
      setTimeout(function() {
         if (!isCancelCount && time > 0) {
            time--
            second = '(' + time + '秒)'
            _this.setData({
               getSmsCodeText: '重新获取' + second
            })
            _this.countDown(time)
         } else {
            isCancelCount = 0
            _this.setData({
               getSmsCodeText: '重新获取' + second
            })
         }
      }, 1000);
   },
   //提示手机认证
   showToast() {
      co.toastAllText('请先手机认证')
   },
   //点击授权
   getWxUserInfo(e) {
      //获取session_key
      var session_key = wx.getStorageSync('session_key')
      //没有session_key
      if (co.empty(session_key)) {
         //请求登录
         app.wxLogin(function(res) {
            console.log("【app.wxLogin登录结果】===", res)
            //获取用户信息
            _this.getUserInfo(e.detail)
         })
         return
      }
      //获取用户信息
      _this.getUserInfo(e.detail)
   },
   //获取用户 或 注册 或更新
   getUserInfo(data) {
      if (co.empty(data.iv) || co.empty(data.encryptedData))
         return
      co.getShareApi('jyhs/Usermg/userRegister', {
         appid: app.Data.app.appid,
         session_key: wx.getStorageSync('session').session_key,
         iv: data.iv,
         encryptedData: data.encryptedData,
         User_Mobile: _data.Verified_Phone,
         Owner_Id: _opt.IID,
         Users_ID: "jyhs"
      }, function (res) {
         app.Data.userInfo = res.data
         //刷新页面
         _this.setData({
            user: res.data
         })
         console.log('app.Data.userInfo===', app.Data.userInfo)
      })
   },
})